﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;

    [ToolboxItem(true), ToolboxBitmap(typeof(ImageComboBox), "YidanSoft.Library.EditorUtility.WinControls.ImageComboBox.bmp")]
    public class ImageComboBox : ComboBoxBase
    {
        private Bitmap[] bitmapsArray;
        private string[] bitmapsNames;
        private ImageList imageList;
        private const int PREVIEW_BOX_WIDTH = 20;
        private bool useImageList;

        public ImageComboBox()
        {
            this.useImageList = false;
            this.InitializeImageComboBox(null, true, null, null);
        }

        public ImageComboBox(ImageList imageList)
        {
            this.useImageList = false;
            this.InitializeImageComboBox(imageList, true, null, null);
        }

        public ImageComboBox(Bitmap[] bitmapsArray, string[] bitmapsNames)
        {
            this.useImageList = false;
            this.InitializeImageComboBox(null, false, bitmapsArray, bitmapsNames);
        }

        public ImageComboBox(Bitmap[] bitmapsArray, string[] bitmapsNames, bool toolBarUse) : base(toolBarUse)
        {
            this.useImageList = false;
            this.InitializeImageComboBox(null, false, bitmapsArray, bitmapsNames);
        }

        protected override void DrawComboBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
        {
            base.DrawComboBoxItem(g, bounds, Index, selected, editSel);
            if (Index != -1)
            {
                Brush brush = new SolidBrush(SystemColors.MenuText);
                if (!this.useImageList)
                {
                    g.DrawImage(this.bitmapsArray[Index], bounds.Left + 2, bounds.Top + 2, 20, bounds.Height - 4);
                }
                else
                {
                    g.DrawImage(this.imageList.Images[Index], bounds.Left + 2, bounds.Top + 2, 20, bounds.Height - 4);
                }
                Pen pen = new Pen(new SolidBrush(Color.Black), 1f);
                g.DrawRectangle(pen, new Rectangle(bounds.Left + 1, bounds.Top + 1, 0x15, bounds.Height - 3));
                Size size = TextUtil.GetTextSize(g, this.Items[Index].ToString(), this.Font);
                int y = bounds.Top + ((bounds.Height - size.Height) / 2);
                g.DrawString(this.Items[Index].ToString(), this.Font, brush, (PointF) new Point(bounds.Left + 0x1c, y));
                brush.Dispose();
            }
        }

        protected override void DrawComboBoxItemEx(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
        {
            base.DrawComboBoxItemEx(g, bounds, Index, selected, editSel);
            if (Index != -1)
            {
                SolidBrush brush = new SolidBrush(SystemColors.MenuText);
                Rectangle rectangle = bounds;
                rectangle.Inflate(-3, -3);
                Pen pen = new Pen(new SolidBrush(Color.Black), 1f);
                g.DrawRectangle(pen, new Rectangle(rectangle.Left + 1, rectangle.Top + 1, 0x15, rectangle.Height - 3));
                if (!this.useImageList)
                {
                    g.DrawImage(this.bitmapsArray[Index], rectangle.Left + 2, rectangle.Top + 2, 20, rectangle.Height - 4);
                }
                else
                {
                    g.DrawImage(this.imageList.Images[Index], rectangle.Left + 2, rectangle.Top + 2, 20, rectangle.Height - 4);
                }
                Size size = TextUtil.GetTextSize(g, this.Items[Index].ToString(), this.Font);
                int y = bounds.Top + ((bounds.Height - size.Height) / 2);
                Rectangle layoutRectangle = new Rectangle(bounds.Left + 0x1f, y, ((bounds.Width - 0x1f) - 12) - 4, y + size.Height);
                g.DrawString(this.Items[Index].ToString(), this.Font, brush, layoutRectangle);
                brush.Dispose();
            }
        }

        protected override void DrawDisableState()
        {
            base.DrawDisableState();
            using (Graphics graphics = base.CreateGraphics())
            {
                using (Brush brush = new SolidBrush(SystemColors.ControlDark))
                {
                    Rectangle clientRectangle = base.ClientRectangle;
                    Rectangle rectangle2 = new Rectangle(clientRectangle.Left, clientRectangle.Top, clientRectangle.Width, clientRectangle.Height);
                    rectangle2.Inflate(-3, -3);
                    graphics.DrawRectangle(SystemPens.ControlDark, new Rectangle(rectangle2.Left + 2, rectangle2.Top + 2, 20, rectangle2.Height - 4));
                    int selectedIndex = this.SelectedIndex;
                    Size size = TextUtil.GetTextSize(graphics, this.Items[selectedIndex].ToString(), this.Font);
                    int y = clientRectangle.Top + ((clientRectangle.Height - size.Height) / 2);
                    Rectangle layoutRectangle = new Rectangle(clientRectangle.Left + 0x1f, y, ((clientRectangle.Width - 0x1f) - 12) - 4, y + size.Height);
                    graphics.DrawString(this.Items[selectedIndex].ToString(), this.Font, brush, layoutRectangle);
                }
            }
        }

        private void InitializeImageComboBox(ImageList list, bool useImageList, Bitmap[] bitmapsArray, string[] bitmapsNames)
        {
            base.DropDownStyle = ComboBoxStyle.DropDownList;
            this.imageList = list;
            this.useImageList = useImageList;
            if (((bitmapsArray != null) && (bitmapsNames != null)) && !useImageList)
            {
                this.bitmapsArray = bitmapsArray;
                this.bitmapsNames = bitmapsNames;
                for (int i = 0; i < bitmapsArray.Length; i++)
                {
                    this.Items.Add(bitmapsNames[i]);
                }
            }
        }

        public void PassMsg(ref Message m)
        {
            base.WndProc(ref m);
        }

        [Browsable(false)]
        public string[] BitmapNames
        {
            get
            {
                return this.bitmapsNames;
            }
            set
            {
                this.bitmapsNames = value;
                if (this.bitmapsNames != null)
                {
                    for (int i = 0; i < this.bitmapsNames.Length; i++)
                    {
                        this.Items.Add(this.bitmapsNames[i]);
                    }
                }
            }
        }

        [Browsable(false)]
        public Bitmap[] Bitmaps
        {
            get
            {
                return this.bitmapsArray;
            }
            set
            {
                this.bitmapsArray = value;
            }
        }

        public ImageList Images
        {
            get
            {
                return this.imageList;
            }
            set
            {
                this.imageList = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ComboBox.ObjectCollection Items
        {
            get
            {
                return base.Items;
            }
        }
    }
}

